<?php defined('SYSPATH') or die('No direct script access.');

 class Controller_Search extends Controller_Base {
      //
	 public function action_index()
	 {

        $query=$this->request->query();

        $keyword=trim($this->request->query('keyword'));

        if(empty($keyword)){

            $this->redirect("/");
        }

        $keyword=Lib_Base::inputStr($keyword);

        if($this->request-> is_ajax()){

            $page=trim($this->request->post("page"));

            $sort=Lib_Base::inputStr(trim($this->request->post('sort')));

        }
        else{

            $page=$this->request->query("page");

            $sort=trim($this->request->query('sort'));

        }

        $pageSize=Kohana::$config->load('item.search')?Kohana::$config->load('item.search'):50;

        $page=$page==""||$page==0?1:intval($page);

        $pageSize=$pageSize==""?20:intval($pageSize);

        $offset=$pageSize*($page-1);
    
		$model=new Model_Search();

        $posts=Cache::instance()->get('search_list_'.$keyword.$sort.$offset, null);

        if(empty($posts)){

            $posts=$model->getSearchInfo($keyword,$sort,$offset,$pageSize);

            Cache::instance()->set('search_list_'.$keyword.$sort.$offset, $posts, 300);

        }

        if($this->request-> is_ajax()){

            foreach ($posts as $key => $value) {

                $posts[$key]['image']=Lib_Image::getPostImage($value['id'],$value['images'],260);
                
            }

            $result=array();

            $result['list']=$posts;

            exit(json_encode($result));

        }

        $total=Cache::instance()->get('search_items_'.$keyword.$sort.$offset, null);

        if(empty($total)){

            $total=$model->getSearchItems($keyword,$sort);

            Cache::instance()->set('search_items_'.$keyword.$sort.$offset, $total, 300);

        }

        $url="/search?keyword=".$keyword;

        if(!empty($type)){

            $url.="&t=".$type;

        }

		$view=View::factory('search/index');

        if(empty($posts)){

            $recommend=$model->recommend();

            $view->recommend=$recommend;
        }

        $this->template->keyword=$keyword; 

        $view->page=$page;
        
        $view->keyword=$keyword;

        $view->sort=$sort;

        $view->url=$url;

        $view->posts=$posts;

        $view->pages=ceil($total/$pageSize);

        $view->total=$total;

		$this->template->content =$view;

        $this->template->title = __('Search-'.$keyword);

	 }

    public function action_keyword(){

        if($this->request-> is_ajax()){

            $result=array();

            $keyword=$this->request->post('keyword');

            if(empty($keyword)){

                $result['code']="0x00001";

                exit(json_encode($result));

            }

            $keyword=Lib_Base::inputStr($keyword);

            $model_search=new Model_Search();

            $model_search->saveInfo($keyword);

            $result['code']="0x00000";

            exit(json_encode($result));

        }

    }
 } 
